Questions
27 of 45
1What is a pseudo-element in CSS?
2How are pseudo-elements different from pseudo-classes?
3What is the syntax of a pseudo-element?
4Name some commonly used pseudo-elements.
5What does the ::before pseudo-element do? What does the ::after pseudo-element do?
6How can you insert content using pseudo-elements?
7What is the difference between :before and ::before? Why were pseudo-elements changed from one colon (:) to two (::) in CSS3?
8What does the ::first-letter pseudo-element do?
9What does the ::first-line pseudo-element do?
10How can you style the first line of a paragraph differently?
11Can pseudo-elements be styled with animations or transitions?
12What’s the difference between using ::before and ::after?
13Can pseudo-elements be positioned using CSS positioning properties? Can pseudo-elements have background images or colors? How can you use pseudo-elements to create shapes or icons?
14Can you use multiple pseudo-elements on the same element?
15What is the ::selection pseudo-element used for?
16How does stacking and z-index affect pseudo-elements?
17Can pseudo-elements have child elements or content inside them?
18Can you apply pseudo-elements to replaced elements (like <img>)?
19How do pseudo-elements interact with display and overflow properties?
20What’s the difference between using pseudo-elements and real elements for decoration?
21How does inheritance work with pseudo-elements?
22Can pseudo-elements trigger JavaScript events (like click)?
23How can you control the generated content with pseudo-elements using attr()?
24Can pseudo-elements be used inside flex or grid layouts?
25How do you control the stacking order of ::before and ::after pseudo-elements?
26How can you create a tooltip using pseudo-elements?
27How can you make a custom underline or highlight effect using ::after?
28How can you make a quotation mark appear before a paragraph using pseudo-elements?
29How do you create a triangle or arrow shape using only CSS pseudo-elements?
30How can you use pseudo-elements to add icons without extra markup?
31How can you create a button hover animation using ::before or ::after?
32How can pseudo-elements help in implementing skeleton loaders or shimmer effects?
33How can you use ::before and ::after to clear floats?
34How can you create a border animation using pseudo-elements?
35How can pseudo-elements be used in responsive design?
36What is the ::marker pseudo-element and when is it used?
37What does the ::placeholder pseudo-element do?
38How does ::cue work with media elements?
39What is ::backdrop, and where is it used?
40What is the difference between ::file-selector-button and other input pseudo-elements?
41How does the ::part() pseudo-element work in Web Components?
42How is ::slotted() different from ::part() in shadow DOM styling?
43Can you style text highlights with pseudo-elements?
44How can ::selection be customized for accessibility and branding?
45What are the limitations of pseudo-elements compared to actual DOM elements?
27 / 45

How can you make a custom underline or highlight effect using ::after?

Creating Custom Underline or Highlight Effects with ::after in CSS

You can use the ::after pseudo-element to create decorative underlines, highlights, or other visual effects beneath text. By styling ::after with position, width, height, and background-color, you can achieve custom designs without extra HTML elements.

Key Points
  1. 1

    Set position: relative on the parent element so the pseudo-element can be positioned absolutely relative to it.

  2. 2

    Use ::after with content: '' and position: absolute to create a visual element behind or below the text.

  3. 3

    Adjust width, height, bottom, and left to control the size and position of the underline or highlight.

  4. 4

    Use background-color or gradients to style the effect, and transitions for animated highlights.

Example: Custom Underline with ::after

In this example, the ::after pseudo-element creates a gold underline that animates from right to left when the paragraph is hovered. This technique allows custom decorative effects without additional HTML elements.

Best Practices
  1. 1

    Use pseudo-elements for decorative underlines or highlights that do not require semantic HTML changes.

  2. 2

    Combine with transitions or animations for interactive effects.

  3. 3

    Adjust position and size carefully to avoid overlapping text or other elements.

  4. 4

    Test across browsers to ensure consistent rendering, especially with scaling or transforms.

Difficulty: 6/10
Topics: pseudo-elements, CSS positioning, visual styling

Scenario Questions

0-2 years experience
  1. 1

    How would you add a subtle yellow highlight behind a single line of text using ::after, ensuring it doesn't push other content around?

  2. 2

    If you use ::after to create an underline but it's not showing up, what are the first three things you'd check?

  3. 3

    You need a 2px bottom underline that starts 5px from the left edge of the text — how would you write the CSS?

2-5 years experience
  1. 1

    A designer wants a dynamic underline that extends beyond the text width on hover — your implementation breaks when the text wraps. How do you fix it?

  2. 2

    We're seeing flickering on mobile when hovering over links with ::after highlights. What could be causing it, and how would you debug it?

  3. 3

    The underline works fine in Chrome but disappears in Safari — what’s likely wrong, and how do you make it cross-browser compatible?

5-8 years experience
  1. 1

    You're building a reusable heading component with customizable underlines — how do you design the CSS to support multiple styles (dashed, wavy, color) without bloating the stylesheet or requiring JS?

  2. 2

    In a high-traffic product, we're using ::after for visual feedback on 50+ interactive elements per page. Is this performant? What metrics would you monitor, and how would you optimize?

  3. 3

    A legacy component uses inline styles and JS to draw underlines — you want to migrate to CSS ::after. What edge cases do you anticipate, and how do you ensure backward compatibility?

8+ years experience
  1. 1

    We're standardizing visual feedback across 20+ products — how do you architect a design system for underlines/hover effects using ::after that scales across themes, languages, and accessibility requirements without becoming a maintenance nightmare?

  2. 2

    A major redesign requires replacing all JS-based underlines with CSS ::after. How do you plan the migration, measure impact on performance and accessibility, and get buy-in from design and QA teams?

  3. 3

    Your team’s underline component is now used in a CMS where users can inject arbitrary HTML. What security or layout risks does ::after introduce, and how do you harden the implementation?

Follow-up Questions

  • What happens if the text wraps to multiple lines?
  • How would you make this work with right-to-left languages?
  • Could this break if the parent has overflow: hidden?